<template>
  <div class="box">
    <swiper>
      <div class=" slide-item" v-for="item in 6" :style="{'background': 'yellow'}" style="height: 50px;" ></div>

    </swiper>
    <HomeFirstClassification :finalList="finalList"></HomeFirstClassification>
  </div>
</template>

<script>
  import HomeFirstClassification from "@/real-components/RegularMealList";
  import Swiper from '@/real-components/SwiperTemplate'
  import {mapGetters} from "vuex";
  import {getPageTree, initDomTree} from "../../../utils/getPageTree";

  import axios from "axios";
  import activeTime from '@/real-components/ActiveTime'
  export default {
    name: 'hello',
    data() {
      return {
        recomendData: [],
        countLength: 0,

        finalList: [
        ],
      }
    },
    computed: {
    },
    components: {
      HomeFirstClassification,
      activeTime,
      Swiper
    },
    methods: {
      queryRecomend() {
        let queryClassifyPic = api.local_host + "/resourceLevel/queryResourceLevel"
        axios({
          url: queryClassifyPic,
          method: "post",
          data: {
            "enterpriseId": api.enterpriseId,
            "pc": false
          }
        }).then((res) => {
          let source = res.data.data[0]
          if (source) {
            for (let key in source) {
              if (source[key].categoryName === '虚拟商品') {
              } else {
                this.countLength += 1
                let titleConfig = source[key].title
                let bannerConfig = source[key].imageList[0]
                let goodsConfig = source[key].prod
                if (source[key].title) {
                  titleConfig.classificationId = source[key].classificationId
                }
                this.renderRecomendCate(titleConfig, bannerConfig, goodsConfig, key)
              }
            }
          }
        })
      },

      renderRecomendCate(titleConfig, bannerConfig, configData, index, classificationId) {
        let ids = ''
        configData && configData.map((item) => {
          if (item.goodsNo) {
            ids += item.goodsNo + ','
          }
        })
        let self = this
        if (ids) {
          axios.get(api.mall_goods_host + '/goodsRestApi/queryGoodsList?enterpriseid=' + api.enterpriseId + "&goodscode=" + ids).then((res) => {
            let dataSource = res.data.data
            dataSource && dataSource.map((item, i) => {
              let val = configData[i]
              var goodsNo = val.goodsNo
              var item = dataSource.find((ele) => {
                  return parseInt(ele.goodscode) == parseInt(goodsNo)
                }) || {}

              //              console.log("index",i,"vae",goodsNo , item.goodscode)
              if (val) {
                val.goodsname = item.goodsname
                val.goodsspec = item.goodsspec
                val.goodsunit = item.goodsunit
                val.wsprice = item.wsprice
                val.normalprice = item.normalprice
              }
            })
            self.recomendData.push({
              recomendTitle: titleConfig,
              bannerImg: bannerConfig,
              goodsList: configData,
              index
            })
            //            console.log("self.recomendData", self.recomendData)
            if (self.countLength == self.recomendData.length) {
              self.sortList()
            }
          })
        } else {
          self.recomendData.push({
            recomendTitle: titleConfig,
            bannerImg: bannerConfig,
            goodsList: [],
            index
          })
          if (self.countLength == self.recomendData.length) {
            self.sortList()
          }
        }
      },

      sortList() {
        let list = this.recomendData
        // console.log("++++++++++++===", list)
        list.sort(this.compare('index'))
        this.finalList = list
      },

      compare(property) {
        return function (a, b) {
          var value1 = a[property];
          var value2 = b[property];
          return value1 - value2;
        }
      },

    },
    created() {

    },
    mounted() {
      this.queryRecomend();
    },
    updated() {
      this.$emit('refresh')
    }
  }
</script>

<style>

</style>
